// pages/circle/add/add.js
const app = getApp();
import { _circle_houses, _circle_houses_img_upload, _circle_issue } from '../../../utils/qiaofinn'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    imgIdList: [],
    houses_picker: [],
    house_index: null,
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _circle_houses(res => {
      this.setData({
        houses_picker: res
      })
    })
  },
  PickerChange(res) {
    this.setData({
      house_index: res.detail.value
    })
  },
  textareaAInput(res) {
    this.setData({
      content: res.detail.value
    })
  },
  ChooseImage() {
    var that = this
    wx.chooseImage({
      count: 6, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        const tempFilePaths = res.tempFilePaths
        for (let i = 0; i < tempFilePaths.length; i++) {
          _circle_houses_img_upload(tempFilePaths[i], reqs => {
            const result = JSON.parse(reqs)
            that.setData({
              imgList: that.data.imgList.concat([tempFilePaths[i]]),
              imgIdList: that.data.imgIdList.concat([result.data.id])
            })
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确认',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.data.imgIdList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList,
            imgIdList: this.data.imgIdList
          })
        }
      }
    })
  },
  issueTap() {
    const { imgIdList, content, house_index, houses_picker } = this.data
    if (!house_index) {
      wx.showToast({
        icon: 'none',
        title: '请选择地址',
      })
      return
    }
    if (!content) {
      wx.showToast({
        icon: 'none',
        title: '请填写内容',
      })
      return
    }
    if (imgIdList.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请至少上传一张照片',
      })
      return
    }
    const data = {
      "content": content,
      "images": imgIdList,
      "house": houses_picker[house_index].id
    }
    _circle_issue(data, res => {
      if (res.status) {
        wx.showToast({
          icon: 'none',
          title: '发布成功',
        })
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})